import { ValueObjectArray } from "./ValueObjectArray";
import { ValueObject } from "./ValueObject";

// test1 测试简单的属性关系绑定
let t1 = new ValueObject(["hp = 10", "friend", "atk = 11"]);
t1.bind("final_atk", "atk + hp * 0.5");
console.log(t1.final_atk); //输出为16
let f1 = new ValueObject(["hp = 100"]);
t1.bind("hp", "friend.hp + 10");
t1.friend = f1;
console.log(t1.final_atk); //输出为66

// test2 vo数组
let pets = new ValueObjectArray(null, ["hp = 10", "def = 3"], 10);
console.log(pets[0].hp, pets[1].def);

// 绑定角色属性和宠物的属性关系
let role = new ValueObject(["atk=10", "pets", "def=9"]);
role.pets = pets;
role.bind("final_atk", "pets.sum(hp)*0.1+atk");
role.bind("final_def", "pets.max(def)+def");
pets[7].def = 6;
pets.push(new ValueObject(["hp=10", "def=10"]));

console.log(role.final_atk); //输出21
console.log(role.final_def); //输出19

// 更改源头数据后，自动更新相应衍生数据
role.pets[3].hp = 100;
role.pets[5].def = 34;
console.log(role.final_atk); //输出30
console.log(role.final_def); //输出43

let vo = new ValueObject(["type", "names"]);
vo.names = ["alpha", "beta", "gama"];
vo.type = 0;
vo.bind("name", "names[type]");
console.log(vo.name);
vo.type = 1;
console.log(vo.name);
vo.type = 2;
console.log(vo.name);

role.bind("star", "pets.length * 3");
console.log(role.star);
pets.push(new ValueObject(["hp=11", "def=3"]));
console.log(role.star);

role.bind("firstPet", "pets.child(0)");
console.log(role.firstPet.hp);
pets[0].hp = 34;
console.log(role.firstPet.hp);
pets[0] = new ValueObject(["hp=999", "def=123"]);
console.log(role.firstPet.hp);
console.log(role.final_atk);

pets.insert(new ValueObject(["hp = 1999", "def = 199"]), 0);
// TODO 数组插入时，后续若干child的index都发生了变化，绑定child(n)的属性要有相应反应
console.log(role.firstPet.hp);

role.bind("secondPet", "pets.child(1)");
for (let i = 0; i < 10; i++) {
    pets.insert(new ValueObject(["hp = " + i, "def = 99"]), 0);
    console.log(role.secondPet.hp);
}

role.bind("lastPet", "pets.child(-1)");
console.log(role.lastPet.hp);
for (let i = 0; i < 10; i++) {
    pets.push(new ValueObject(["hp = " + i, "def = 99"]));
    console.log(role.lastPet.hp);
}